home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 July / EnigmA AMIGA RUN 09 (1996)(G.R. Edizioni)(IT)[!][issue 1996-07 & 08][EARSAN CD VIII].iso / earcd / util3 / fiflb382.lha / handler.h < prev    next >
C/C++ Source or Header  |  1996-05-13  |  6KB  |  212 lines

  1.  
  2. /*
  3.  *  HANDLER.H
  4.  */
  5.  
  6. /*  useful with GCC macro-based inlines */
  7. #define __CONSTLIBBASEDECL__ const
  8.  
  9. #include <exec/types.h>
  10. #include <exec/nodes.h>
  11. #include <exec/ports.h>
  12. #include <exec/interrupts.h>
  13. #include <exec/memory.h>
  14. #include <exec/alerts.h>
  15. #include <exec/execbase.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/filehandler.h>
  19. #include <devices/timer.h>
  20. #include <libraries/fifo.h>
  21.  
  22. extern struct ExecBase   * SysBase;
  23. extern struct DosLibrary * DOSBase;
  24.  
  25. #include <proto/fifo.h>
  26. #if defined(_DCC) /*  at least the very old DICE 2.06 */
  27. #include <clib/dos_protos.h>
  28. #include <clib/exec_protos.h>
  29. #include <clib/alib_protos.h>
  30. #else /*  proto/ will include inline/ if optimization is on in GCC */
  31. #include <proto/dos.h>
  32. #include <proto/exec.h>
  33. #include <proto/alib.h>
  34. #endif
  35.  
  36. #ifdef DEBUG
  37. #include <stdio.h>        /*  vsprintf() */
  38. #include <stdarg.h>        /*  for xprintf() */
  39. #endif
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. #if defined(__GNUC__)
  44. #define inline __inline__
  45. #else
  46. #define  inline
  47. #endif
  48.  
  49. #if defined(__GNUC__)
  50. /*  We can make GCC generate very nice code with a little help! */
  51. #if 0
  52. #include <inline/strsup.h>    /*  inline strlen() etc. */
  53. #else
  54. extern inline int strlen(const char *string)
  55. {
  56.     const char *s=string;
  57.     while(*s++) ;        /*  tstb a0@+; bne 0b */
  58.     return ~(string-s);
  59. }
  60.  
  61. /*  Do not use this inline for strcpy(foo, "...") which is optimized away */
  62. extern inline char *strcpy(char *d, const char *s)
  63. {
  64.     char *d2 = d;
  65.     /*
  66.      *  GCC would generate "0: moveb a@+,d0; move d0,a@+, jne 0b" instead
  67.      */
  68.     __asm__ volatile ("0: moveb %0@+,%1@+; jne 0b"
  69.     : "=a" (s), "=a" (d) : "0" (s), "1" (d) : "cc", "memory");
  70.     return d2;
  71. }
  72.  
  73. /*
  74.  *  GCC-1.x, 2.3.3, 2.5.8, 2.6.3, 2.7.0 use dbra
  75.  *  in such while((short|long)(--n) != -1) loops
  76.  *  Other loops forms may not lead to the same optimization with all GCCs
  77.  *  short count generates single dbra, long count dbra in the inner loop
  78.  */
  79. extern inline char *strscpy(char *d, const char *s, short n)
  80. {
  81.     char *d2 = d;
  82.     if (n) {
  83.     --n;
  84.     /*  0: moveb a0@+,d0; (grr) moveb d0,a0@+; beq 1f; dbra d0,0b; 1: */
  85.     while ((*d++=*s++) && ((short)(--n) != -1)) ;
  86.     }
  87.     return d2;
  88. }
  89. /*  short is enough for fifo-handler */
  90. #define strncpy strscpy
  91.  
  92. /*
  93.  *  defines, not inline functions, work even without optimization
  94.  *  and work even better than inline function with optimization on
  95.  */
  96. #define memscpy(d,s,scount)  \
  97. ({  void *__memscpy_d = (d); \
  98.     void *__memscpy_s = (s); \
  99.     short __memscpy_scount = (scount); \
  100.     void *__memscpy_res = __memscpy_d; \
  101.     if (!(__memscpy_scount==0)) { \
  102.     --__memscpy_scount; \
  103.     do { *(char*)__memscpy_d++ = *(char*)__memscpy_s++; } \
  104.     while ((short)(--__memscpy_scount)!=-1); \
  105.     } \
  106.     __memscpy_res; \
  107. })
  108. /*
  109.  *  The following redefine is safe because: 1st count is small,
  110.  *  2nd this memscpy() can still copy overlapping memory to
  111.  *  lower addresses and this is exactly what fifo-handler needs
  112.  */
  113. #define memmove(d,s,count)  memscpy(d,s,count)
  114.  
  115. /*  BUG: wrong sign for chars > 127 */
  116. #define streql(s1,s2)  \
  117. ({  char *__streql_s1 = (s1), *__streql_s2 = (s2); \
  118.     char __streql_c1, __streql_c2; \
  119.     while (!(__streql_c1=*__streql_s1++ \
  120.          -(__streql_c2=*__streql_s2++)) \
  121.        && __streql_c2) ; \
  122.     __streql_c1; \
  123. })
  124. /*  the sign bug is not important, only 0 counts in fifo-handler */
  125. #define strcmp(s1,s2)  streql(s1,s2)
  126. #endif /*  0        */
  127. #endif /*  __GNUC__ */
  128.  
  129. #ifndef memmove
  130. #define memmove(to,from,count)  bcopy(from,to,count)
  131. #endif
  132.  
  133. #define Prototype extern
  134.  
  135. #define DOS_TRUE    -1
  136. #define DOS_FALSE   0
  137.  
  138. #define CB_SIZE     1024    /*  lines buffer */
  139. #define FIFO_SIZE   2048
  140.  
  141. #define BTOC(bptr)  ((void *)((unsigned long)(bptr) << 2))
  142. #define CTOB(cptr)  ((BPTR)(((unsigned long)cptr) >> 2))
  143.  
  144. #define SIGS    (SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)
  145.  
  146. typedef struct DosPacket    DosPacket;
  147. typedef struct FileHandle   FileHandle;
  148. typedef struct DeviceNode   DeviceNode;
  149. typedef struct Process        Process;
  150. typedef struct MinNode        Node;
  151. typedef struct MinList        List;
  152. typedef struct Node        MaxNode;
  153. typedef struct List        MaxList;
  154. typedef struct MsgPort        MsgPort;
  155. typedef struct Message        Message;
  156. typedef struct Interrupt    Interrupt;
  157. typedef struct DosList        DosList;
  158. typedef struct RootNode     RootNode;
  159. typedef struct DosInfo        DosInfo;
  160.  
  161. typedef struct SharRead {
  162.     void    *sr_FifoR;
  163.     long    sr_Refs;
  164. } SharRead;
  165.  
  166. typedef struct FHan {
  167.     MaxNode    ff_Node;    /*    fifo node, ln_name holds fifo name */
  168.     short    ff_Flags;
  169.     short    ff_Refs;    /*    open refs   */
  170.     SharRead    *ff_SRead;  /*    fifo handle for reading */
  171.     void    *ff_FifoW;  /*    fifo handle for writing */
  172.     long    ff_FHBufSiz;/*    FifoW half buffer size    */
  173.     Message    ff_RdMsg;
  174.     Message    ff_WrMsg;
  175.     MsgPort    *ff_Port;   /*    unique port for messages    */
  176.     List    ff_RdWait;
  177.     List    ff_WrWait;
  178.     char    *ff_CookBuf;/*    cooked buffer handling        */
  179.     short    ff_CookIdx;
  180.     short    ff_LRet;
  181.     MsgPort    *ff_SigPort;/*    message port instead of task to signal */
  182. } FHan;
  183.  
  184. #define ff_FifoR    ff_SRead->sr_FifoR
  185.  
  186. #define FHF_COOKED    0x0001
  187. #define FHF_RPEND    0x0002
  188. #define FHF_WAVAIL    0x0004
  189. #define FHF_READ    0x0008
  190. #define FHF_WRITE    0x0010
  191. #define FHF_CLOSEEOF    0x0020
  192. #define FHF_MASTER    0x0040
  193. #define FHF_TEE     0x0080
  194. #define FHF_SHELL    0x0100
  195. #define FHF_COOKBFUL    0x0200        /*    cooked buffer is full    */
  196. #define FHF_COOKECHOBLK 0x0400        /*    blocked echoing chars!    */
  197. #define FHF_REOF    0x0800        /*    REMOTE EOF        */
  198. #define FHF_WIHOLD    0x1000        /*    write hold due to input */
  199. #define FHF_COOKCRLF    0x2000        /*    need to write CRLF    */
  200. #define FHF_RREQUIRED    0x4000
  201. #define FHF_STARTCOOK    0x8000        /*    start cooked processing */
  202.  
  203. typedef struct WaitTreq {
  204.     struct timerequest    wt_timereq;
  205.     DosPacket        *wt_packet;
  206. } WaitTreq;
  207.  
  208. /* Were Protoize used
  209. #include "fifohan-protos.h"
  210. */
  211.  
  212.